/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.framework.type;

import axil.api.AxilObject;
import axil.api.AxilType;
import axil.api.error.AxilException;

import static axil.framework.Functions.*;


/**
 * This base class is used for objects that do not really represent a value,
 * but represent something internal to Axil itself, such as a function or opcode.
 * Therefore, most of the capabilities of an Axil object have a common
 * implementation.
 */
public abstract class SpecialValue implements AxilObject, AxilType {
	/**
	 * Get the type metadata for this object. The type metadata contains
	 * information about the type itself, as an object. Since these objects are
	 * special and not intended to be created by scripts, they are not
	 * registered types.
	 */
	public AxilType type() {
		return this;
	}


	/**
	 * Provide a description of the type of this object. The string returned is
	 * the Axil type name, not the Java name for any implementation. For example,
	 * "integer", not "int" or "Integer".
	 */
	public String identity() {
		return "(special)";
	}


	/**
	 * Transform the given native object into its equivalent value object. The
	 * object given is never null and is guaranteed to be one of the alias types
	 * indicated by this object's type (as reported by the type() method).
	 * If the object is one of the common types, then that indicator is also
	 * given.
	 */
	public AxilObject transform(Object object) {
		return null;
	}


	/**
	 * Tell if this object, represents a true or false value. For this special
	 * object, its not null, so we default to true.
	 */
	public boolean bool() {
		return true;
	}


	/**
	 * Tell if this object is exactly equal to the given object. Exact equality
	 * takes type, order and all aspects of the object into consideration. An
	 * object can only be exactly equal to another if it is the exact same type.
	 * The object given is never null. The object given may be of any type of
	 * value object. If the given object is not a suitable type for comparison,
	 * a ClassCastException may be thrown.
	 */
	public boolean exactly(AxilObject object) {
		return this == object;
	}


	/**
	 * Tell if this object is roughly equivalent to the given object. That is,
	 * would a non-technical person consider the objects equivalent? For example,
	 * a list(1,2,3) is equivalent to the list(3,2,1), but those lists are not
	 * exactly equal. The object given is never null. The object given may be
	 * of any type of value object. If the given object is not a suitable type
	 * for comparison, a ClassCastException may be thrown.
	 */
	public boolean equivalentTo(AxilObject object) {
		return this == object;
	}


	/**
	 * Tell if this object is equivalent to the given object. The object given
	 * is never null. The object given may be of any type of value object.  If
	 * the given object is not a suitable type for comparison, a
	 * ClassCastException may be thrown.
	 */
	public boolean equalTo(AxilObject object) {
		return this == object;
	}


	/**
	 * Indicates whether some other object is equal to this one.
	 */
	public boolean equals(Object object) {
		if (object == null) {
			return false;
		}
		return this.equalTo((AxilObject)object);
	}


	/**
	 * Compares this object with the specified object for order. If the given
	 * object is not a suitable type for comparison, a ClassCastException may
	 * be thrown.
	 *
	 * @param object
	 * 	The object to compare against. The given object cannot be null but may
	 * 	be any Axil object.
	 *
	 * @return
	 * 	Returns a negative integer, zero, or a positive integer as this object
	 * 	is less than, equal to, or greater than the specified object.
	 */
	public int comparedTo(AxilObject object) {
		return 0;
	}


	/**
	 * Compares this object with the specified object for order.  Returns a
	 * negative integer, zero, or a positive integer as this object is less
	 * than, equal to, or greater than the specified object.
	 */
	public int compareTo(AxilObject object) {
		return 0;
	}


	/**
	 * Parse the given text as a representation of the canonical form of this
	 * type. A null is returned if the text does not represent a valid object.
	 * The string given cannot be null.
	 */
	public AxilObject parse(String text) {
		return null;
	}


	/**
	 * Return the host application (or built-in Java) object that most closely
	 * matches this Axil object.
	 *
	 * @return
	 * 	Return returned value is never null unless this Axil object represents
	 * 	the special 'nil' object.
	 *
	 * 	@see axil.api.Axil#NIL
	 */
	public Object intrinsic() {
		return this;
	}


	/**
	 * Coerce this object into an instance of the given type. If this object
	 * cannot be meaningfully represented as an instance of that type, then an
	 * exception is thrown.
	 *
	 * @param type
	 * 	The type to be coerced to. The object given cannot be null.
	 *
	 * @return
	 * 	Returns an instance of the given type. If this object is nil, then nil
	 * 	is returned.
	 */
	public AxilObject coerce(AxilType type) throws AxilException {
		if (type == type()) {
			return this;
		}
		throw error(axil(), "cannot-coerce-object",
		            nv("from", type()), nv("to", type));
	}


	/**
	 * Return a string representation of this object. The string returned is
	 * never null.
	 */
	public String toString() {
		return "(special)";
	}
}
